package com.example.expand_list_view;

import static com.xuexiang.xui.XUI.getContext;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.expand_list_view.adapter.MyExpandListAdapter;
import com.example.expand_list_view.beans.School;
import com.example.expand_list_view.beans.Student;
import com.xuexiang.xui.widget.popupwindow.easypopup.EasyPopup;
import com.xuexiang.xui.widget.popupwindow.easypopup.HorizontalGravity;
import com.xuexiang.xui.widget.popupwindow.easypopup.VerticalGravity;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private ExpandableListView listView;
    private ArrayList<School> schools;
    private List<ArrayList<Student>> childData;

    private EasyPopup easyPopup ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        listView = findViewById(R.id.expand_list_view);
        schools = getGroups();
        childData = getChildData();
        initAdapter(schools, childData,this);

        //屏蔽分组的点击事件，让他无法折叠
        listView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
            @Override
            public boolean onGroupClick(ExpandableListView expandableListView, View view, int i, long l) {
                return true;
            }
        });

        listView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            //返回值不知道是干嘛的
            @Override
            public boolean onChildClick(ExpandableListView expandableListView, View view, int groupPosition, int childPosition, long l) {
                //Toast.makeText(MainActivity.this, "你点击了：" + childData.get(groupPosition).get(childPosition).getName(), Toast.LENGTH_SHORT).show();
                //点击item让easyPop弹出快来
                /**
                 * VerticalGravity.BELOW 表示在item的下面进行弹出
                 * HorizontalGravity.CENTER 表示在item的水平中间显示
                 * x y 水平和垂直方向的偏移
                 */
                easyPopup.showAtAnchorView(view ,  VerticalGravity.BELOW, HorizontalGravity.CENTER , 0,0);
                return true;
            }
        });

        //取消groupItem左侧的箭头
        listView.setGroupIndicator(null);

        //让分组0展开
        listView.expandGroup(0);
        //通过这个代码可以让指定的分组折叠
        //listView.collapseGroup(0);
        listView.expandGroup(1);
        listView.expandGroup(2);


        //无论是group还是childItem都计数，滚动到指定位置
        listView.smoothScrollToPosition(13);


        //初始化弹出框
        intEasyPop();

    }

    private List<ArrayList<Student>> getChildData() {

        ArrayList<ArrayList<Student>> childData = new ArrayList<>();

        ArrayList<Student> studentsInSit = new ArrayList<>();
        ArrayList<Student> studentsInHNKJ = new ArrayList<>();
        ArrayList<Student> studentsInZS = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            Student student = new Student("SIT-"+"student-"+i);
            studentsInSit.add(student);

            Student student2 = new Student("HNKJXY-"+"student-"+i);
            studentsInHNKJ.add(student2);

            Student student3 = new Student("ZS-"+"student-"+i);
            studentsInZS.add(student3);
        }

        childData.add(studentsInSit);
        childData.add(studentsInHNKJ);
        childData.add(studentsInZS);
        return childData;

    }

    private ArrayList<School> getGroups() {
        ArrayList<School> arrayList =new ArrayList();
        School school1 = new School("上海应用技术大学");
        School school2 = new School("河南科技学院");
        School school3 = new School("郑州商学院");
        arrayList.add(school1);
        arrayList.add(school2);
        arrayList.add(school3);
        return arrayList;
    }

    public void initAdapter(List<School> groups, List<ArrayList<Student>> childData, Context context){
        MyExpandListAdapter myExpandListAdapter =new MyExpandListAdapter( groups,childData,context);
        listView.setAdapter(myExpandListAdapter);
    }

    private void intEasyPop(){
        easyPopup = new EasyPopup(getContext())
                .setContentView(R.layout.pop_layout)
                .setFocusAndOutsideEnable(true)
                .createPopup();

        //拿到R.layout.pop_layout组件的id
        TextView tv_one = easyPopup.getView(R.id.one);
        tv_one.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(getContext(), "one" , Toast.LENGTH_SHORT).show();
                //让pop消失
                easyPopup.dismiss();
            }
        });
    }

}